<?php
class baiduMapClient
{    
    private $api_server_url;
    private $auth_params;

    public function __construct()
    {
        $this->api_server_url = "http://api.map.baidu.com/";
        $this->auth_params = array();
           $this->auth_params['key'] = "401f9a693dd267dd9a4661ec0895fb20";
        $this->auth_params['output'] = "json";
    }

    public function Geocoding_coordinate_address($location) 
    {   
        return $this->call("geocoder", array("location" => $location));
    }
    
    //http://api.map.baidu.com/place/search?&query=眼镜&location=39.915,116.404&radius=3000&output=json&key=37492c0ee6f924cb5e934fa08c6b1676
    public function Place_search($query, $location, $radius) 
    {
        return $this->call("place/search", array("query" => $query, "location" => $location, "radius" => $radius));
    }
    
    protected function call($method, $params = array())
    {
        $headers = array(
            "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language: en-us,en;q=0.5",
            //"Accept-Encoding: gzip, deflate",
            "Referer: http://developer.baidu.com/"
        );
        $params = array_merge($this->auth_params, $params);
        $url = $this->api_server_url . "$method?".http_build_query($params);
        #if (DEBUG_MODE){echo "REQUEST: $url" . "\n";}
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
         $data = curl_exec($ch);
        curl_close($ch);    
        $result = null;
        if (!empty($data)){
            #if (DEBUG_MODE){
                #echo "RETURN: " . $data . "\n";
            #}
            $result = json_decode($data);
        }
        else{
            echo "cURL Error:". curl_error($ch);
        }
        return $result;
    }
}